package org.ws.lifebacklog.view;

import org.ws.lifebacklog.R;

import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * @author sergey.valuy
 * 
 */
public class ActionTabView extends RelativeLayout {

	private View view;
	private FrameLayout content;
	private LinearLayout header;
	private LocalActivityManager mActivityManager;

	public ActionTabView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);

	}

	public ActionTabView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);

	}

	public ActionTabView(Context context) {
		super(context);
		init(context);
	}

	private void init(Context context) {
		view = inflate(context, R.layout.view_action_tabs, this);
		header = (LinearLayout) view.findViewById(R.id.action_tab_header);
		content = (FrameLayout) view.findViewById(R.id.action_tab_content);
	}

	public void setup(LocalActivityManager activityManager) {
		mActivityManager = activityManager;

	}

	public void addItem(int textId, int iconId, final Class<? extends Activity> clazz) {
		View heder = inflate(getContext(), R.layout.view_tab_header, null);
		((ImageView) heder.findViewById(R.id.tab_icon)).setImageResource(iconId);
		((TextView) heder.findViewById(R.id.tab_name)).setText(textId);
		heder.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Window w = mActivityManager.startActivity(clazz.getName(), new Intent(getContext()
						.getApplicationContext(), clazz));
				content.removeAllViews();
				content.addView(w.getDecorView());

			}
		});
		header.addView(heder);
	}
}
